package com.fjec.devops.schedule;

import com.fjec.common.util.JsonUtil;
import com.fjec.devops.schedule.handler.*;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class DevopsSchedule {

    @Resource
    private StatShopHandler statShopHandler;

    @Resource
    private StatOrderHandler statOrderHandler;

    @Resource
    private StatPersonHandler statPersonHandler;

    @Resource
    private YsOffLineRegHandler ysOffLineRegHandler;

    @Resource
    private YsOffLineRegResultHandler ysOffLineRegResultHandler;

    /**
     * 统计信息处理
     *
     * @param param
     * @return
     */
    @XxlJob("statDataHandler")
    public ReturnT<String> statDataHandler(String param) {
        Integer days = Integer.valueOf(param);
        // 处理商铺
        Map<String, Object> shopMap = statShopHandler.handle(days);
        // 处理人数
        Map<String, Object> personalMap = statPersonHandler.handle(days);
        // 处理订单
        String orderLog = statOrderHandler.handle(days);
        // 记录日志
        Map<String, Object> logMap = new HashMap<>();
        logMap.put("order", orderLog);
        logMap.putAll(shopMap);
        logMap.putAll(personalMap);
        String log = JsonUtil.toJson(logMap);
        XxlJobLogger.log(log);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("statDataHandler");
        return returnT;
    }

    /**
     * 线下进件文件生成
     *
     * @param param
     * @return
     */
    @XxlJob("ysOffLineRegHandler")
    public ReturnT<String> ysOffLineRegHandler(String param) {
        Integer days = Integer.valueOf(param);
        String log = ysOffLineRegHandler.handle(days);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("ysOffLineRegHandler");
        return returnT;
    }

    /**
     * 线下进件结果处理
     *
     * @param param
     * @return
     */
    @XxlJob("ysOffLineRegResultHandler")
    public ReturnT<String> ysOffLineRegResultHandler(String param) {
        // 处理参数
        String[] args = param.split(",");
        int limit = Integer.valueOf(args[0]).intValue();
        int numPerTimes = Integer.valueOf(args[1]).intValue();
        String log = ysOffLineRegResultHandler.handle(limit, numPerTimes);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("ysOffLineRegHandler");
        return returnT;
    }

    @Resource
    private ExchangeGiftExcelHandler exchangeGiftExcelHandler;

    /**
     * 生成兑换礼物列表excel
     *
     * @param param
     * @return
     */
    @XxlJob("exchangeGiftExcelHandler")
    public ReturnT<String> exchangeGiftExcelHandler(String param) {
        // 处理参数
        int days = Integer.valueOf(param).intValue();
        String log = exchangeGiftExcelHandler.handle(days);
        ReturnT<String> returnT = ReturnT.SUCCESS;
        returnT.setMsg(log);
        returnT.setContent("exchangeGiftExcelHandler");
        return returnT;
    }
}
